<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Schema;

use App\Model\File;
use Hyperf\Swagger\Annotation\Property;
use Hyperf\Swagger\Annotation\Schema;
use JsonSerializable;

#[Schema(title: '文件详情')]
class FileSchema implements JsonSerializable
{
    #[Property(property: 'id', title: '编号', type: 'int')]
    public ?int $id;

    #[Property(property: 'storage', title: '存储位置', type: 'string')]
    public ?string $storage;

    #[Property(property: 'filename', title: '文件名称', type: 'string')]
    public ?string $filename;

    #[Property(property: 'path', title: '路径', type: 'string')]
    public ?string $path;

    #[Property(property: 'upload_user', title: '上传者', type: 'int')]
    public ?int $uploadUser;

    #[Property(property: 'created_at', title: '创建时间', type: 'string')]
    public string $createdAt;

    #[Property(property: 'updated_at', title: '修改时间', type: 'string')]
    public string $updatedAt;

    public function __construct(File $model)
    {
        $this->id = $model->id;
        $this->storage = $model->storage;
        $this->filename = $model->filename;
        $this->path = $model->path;
        $this->uploadUser = $model->upload_user;
        $this->createdAt = $model->created_at?->toDateTimeString();
        $this->updatedAt = $model->updated_at?->toDateTimeString();
    }

    public function jsonSerialize(): mixed
    {
        return ['id' => $this->id, 'storage' => $this->storage, 'filename' => $this->filename, 'path' => $this->path, 'upload_user' => $this->uploadUser, 'created_at' => $this->createdAt, 'updated_at' => $this->updatedAt];
    }
}
